\section{\TikZ\ (Part 1)}

This is the first video of a three part series discussing the \TikZ\ graphics package. Amusingly, \TikZ\ is a recursive acronym for ``\TikZ\ is not a drawing program'' even though we use it for drawing. It's just not a drawing program in the traditional sense. There is no point-and-click interface and everything is done through code. \TikZ\ is built on another set of commands known as \PGF, which stands for portable graphics format. But we will only be interacting with the higher level TikZ commands. 

This is by no means a full \TikZ\ tutorial. This is just a quick introduction to the basic mechanics of drawing with \TikZ. At the end of the series, I'll point you to some more resources to help you go further.

This video will talk about drawing lines and curves, and then talk about nodes. 

\begin{center}
  \fbox{\includegraphics[width=0.95\linewidth]{img-10-1}}
\end{center}

To start off, we need to load the \href{https://www.ctan.org/tikz}{\TikZ\ package} and create a \texttt{tikzpicture} environment. The first thing to understand is that a \texttt{tikzpicture} is an inline object. That is, you can put a \texttt{tikzpicture} right in the middle of a paragraph. Most of the time, this isn't what you want to do, and you just put it in its own paragraph. I usually have my drawings centered as well, but that's completely up to you.

\begin{center}
  \fbox{\includegraphics[width=0.95\linewidth]{img-10-2}}
\end{center}

A \TikZ\ drawing is built around the standard two dimensional coordinate system, with positive $y$ values going up. The default unit of measure is centimeters. The most basic thing to do is to draw lines, which is done by typing \verb|\draw| followed by a coordinate, a double dash, another coordinate, and finally a semicolon. A semicolon is required at the end of every command. You should interpret the double dash as a line connecting the two coordinates. The line on the right is what the final image would actually look like.

\begin{center}
  \fbox{\includegraphics[width=0.95\linewidth]{img-10-3}}
\end{center}

We can extend our drawing to connect more points together by stringing together a sequence of points separated by double dashes. If you want to close the shape, use \verb|cycle| as the last point. This is better than manually matching up the first and last points for several reasons. The most obvious one is that it's one less thing to change if you adjust your coordinates.

Earlier, we had said that there must be a semicolon at the end of every command. If you string multiple points together like this, it's viewed as a single \verb|\draw| command, and so you only need a semicolon at the end of the sequence. In this example, the indentation and putting our new coordinates on a new line is done for organizational purposes. You could technically string this together as one long line, but it becomes more difficult to read and more difficult to work with as the commands get more complex.

\begin{center}
  \fbox{\includegraphics[width=0.95\linewidth]{img-10-4}}
\end{center}

In addition to rectangular coordinates, you can also use polar coordinates. The notation is \verb|(angle:radius)|. There is a way to use the \texttt{calc} tikzlibrary to calculate the rectangular coordinates to these points, but the notation is a bit fussy and I have not found it to be worth the struggle. And sometimes, using polar coordinates is just the most natural way to do things.

\begin{center}
  \fbox{\includegraphics[width=0.95\linewidth]{img-10-5}}
\end{center}

Another useful tool is to use relative coordinates. Instead of giving the next coordinate in absolute terms, if you use a double plus, you will use a relative coordinate system where your previous point is the origin. It can sometimes be easier to think of your drawings in this way instead of having to calculate the absolute coordinates for all your points. If you wanted to make the same shape in a couple different locations, you would only need to change one coordinate to create it.

A nice feature of the relative coordinates is that if you omit the double dash, you can move your pen without drawing a connecting line. I use this sometimes when trying to get a line to start in the right place if it's unnecessarily difficult or annoying to calculate the coordinates.

\begin{center}
  \fbox{\includegraphics[width=0.95\linewidth]{img-10-6}}
\end{center}

The next thing we might want to do is draw shapes. If you're drawing a rectangle, you do not need to draw all four sides separately. There's a \texttt{rectangle} command that will draw the rectangle based on the coordinates of the two opposite corners.

We can also draw circles and ellipses. Unfortunately, there are two notations that you're going to see out there. The old notation is probably still the most common, but I would steer you towards the newer notation because it's easier to understand, even though it's more typing. However, both notations will be shown so that you can become familiar with both.

Here are the two circle commands. The point refers to the center, and the radius is the other part. To make an ellipse, you have to specify the measurements of both the horizontal and vertical axes. Notice that the old notation uses round parentheses but the new notation uses square brackets.

\begin{center}
  \fbox{\includegraphics[width=0.95\linewidth]{img-10-7}}
\end{center}

To get an arc, you have to use the \texttt{arc} command. However, the point used in the arc command is not the center of the circle or ellipse, but the initial point. In the old notation, the order of the parameters is the initial angle, the terminal angle, and the radius or radii. The new notation lists the values out explicitly and the order doesn't matter. For ellipses, the angles are not to the center of the ellipse, but rather the angles relative to the circle that got stretched out into the ellipse. We're not going to discuss the details of this. If you end up needing to draw elliptical arcs, the drawing in red on the right is your guide.

We're now ready to move on to nodes. A node is container for displaying \LaTeX\ objects. Most of the time, this is used to contain text or equations, but it's actually a very flexible container that can hold many types of objects, including imported images and even other tikzpictures.

\begin{center}
  \fbox{\includegraphics[width=0.85\linewidth]{img-10-8}}
\end{center}

But we'll start with the basics. To draw a node, you need to specify the location of the node and create a container. Whatever you put in the container is what will be displayed at that point.

There are other ways you can create nodes, such as using the \verb|\node| command or \verb|\path| or \verb|\coordinate|, and you can also give nodes names and use them as another way to indicate locations. But that's going to introduce another layer of complexity that I don't think is important right away, so I'll stick with just the \verb|\draw| command. 

Nodes can be created at isolated points, but they can also be built along the way as you're moving from one coordinate to the next in a sequence. You don't even have to be drawing a line or curve between them. However, you will notice that the lines and text overlap each other if you do this. There are a couple things we can do about that.
\begin{itemize}
    \item One thing we can do is to fill the background of the container in the color white. This will block out anything that's already been drawn. \\
    \begin{center}
      \fbox{\includegraphics[width=0.95\linewidth]{img-10-9}}
    \end{center}
    \item We can also change the position of the node's anchor. The default is to put the center of the text just above the coordinate. But we can move the container in any of these eight directions. There's another notation for this, thinking about the node's coordinate as an anchor point, and then describing the corner of the text box that you want the anchor point to be. You can also use the center as an anchor point. I usually use this notation, but I've noticed that people find it to be counter-intuitive. Pick the one that makes sense to you.
\end{itemize}

\begin{center}
  \fbox{\includegraphics[width=0.65\linewidth]{img-10-10}}
\end{center}

This is a good point to talk about how \TikZ\ images are put together. \TikZ\ is executing the drawing code from top to bottom. That means that the order of the code matters. If we have multiple objects drawn in the same space, the one that we want on top of the pile should be the last one in the code.

And this will probably get you through most of what you need to do in \TikZ. But if this was the extent of your knowledge, you may find that your diagrams are just a bit plain. In the next video, we're going to take a tour of the many options that you have that can change the look and feel if your \TikZ\ images.